package com.oig.sys.auth.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupChange;
import com.oig.sys.auth.dto.ResourceColumnDto;
import com.oig.sys.auth.dto.ResourceQltmpDto;
import com.oig.sys.auth.service.ITbResourceColumnService;
import com.oig.sys.auth.service.ITbResourceQltmpService;
import com.oig.sys.auth.vo.ResourceColumnVo;
import com.oig.sys.auth.vo.ResourceQltmpVo;
import com.oig.sys.auth.vo.UserResourceInfoVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.support.SecurityUser;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "用户个性化列操作")
@RestController
@RequestMapping("/userResource")
@Slf4j
public class UserResourceController {

    private final ITbResourceColumnService resourceColumnService;
    private final ITbResourceQltmpService resourceQltmpService;

    @Autowired
    public UserResourceController(ITbResourceColumnService resourceColumnService,
                                  ITbResourceQltmpService resourceQltmpService){
        this.resourceColumnService=resourceColumnService;
        this.resourceQltmpService = resourceQltmpService;
    }

    @Operation(summary = "获取个性化列,如果当前用户有个性化列表，返回个性化展示列，否则返回通用的先,")
    @PostMapping("/findByResourceId/{resourceId}")
    public UserResourceInfoVo findByResourceId(@PathVariable("resourceId") Integer resourceId){
        log.info("query user qltmp and user column:{}", resourceId);
        SecurityUser user = SecurityUtil.getDefineUserMain();
        UserResourceInfoVo resourceInfoVo = new UserResourceInfoVo();
        resourceInfoVo.setResourceColumnList(this.resourceColumnService.findAllColumnByResourceId(resourceId,user.getId(),true));
        resourceInfoVo.setResourceQltmpList(this.resourceQltmpService.findAllQltmpByResourceId(resourceId,user.getId()));
        return resourceInfoVo;
    }


    @SysLog(value="调整个性化列",apiUrl="/userResource/changeAllColumn")
    @Operation(summary = "调整个性化列,全部调整后保存")
    @PostMapping("/changeAllColumn")
    public List<ResourceColumnVo> changeAllColumn(@RequestBody @Validated(GroupChange.class) List<ResourceColumnDto> resourceColumnDtoList){
        log.info("change all Column:{}", resourceColumnDtoList.toString());
        return resourceColumnService.changeAllColumn(resourceColumnDtoList);
    }

    @SysLog(value="保存用户万能查询方案",apiUrl="/userResource/saveUserQlt")
    @Operation(summary = "保存用户万能查询方案,个人独有的")
    @PostMapping("/saveUserQlt")
    public ResourceQltmpVo saveUserQlt(@RequestBody @Validated ResourceQltmpDto resourceQltmpDto){
        log.info("save UserQlt :{}",resourceQltmpDto);
        return resourceQltmpService.saveResourceQltmp(resourceQltmpDto);
    }

    @SysLog(value="用户删除万能查询方案",apiUrl="/userResource/delUserQlt")
    @Operation(summary = "用户删除万能查询方案,个人独有的")
    @PostMapping("/delUserQlt/{qltId}")
    public SysResponse<Integer> delUserQlt(@PathVariable(value = "qltId")Integer qltId){
        log.info("del UserQlt :{}",qltId);
        this.resourceQltmpService.delUserQlt(qltId);
        return SysResponse.ok(qltId, "删除成功");
    }

}
